package org.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.example.entity.Permission;
import org.example.entity.RolePermission;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 权限Mapper接口
 */
@Mapper
public interface PermissionMapper extends BaseMapper<Permission> {
    
    /**
     * 根据用户ID查询用户权限编码列表（平台端，不考虑company_id）
     */
    @Select("SELECT DISTINCT p.permission_code " +
            "FROM permissions p " +
            "INNER JOIN role_permissions rp ON p.id = rp.permission_id " +
            "INNER JOIN roles r ON rp.role_id = r.id " +
            "INNER JOIN user_role_platform_relations ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.is_active = 1 AND r.is_active = 1")
    List<String> getPlatformPermissionsByUserId(@Param("userId") Long userId);
    
    /**
     * 根据用户ID和公司ID查询用户权限编码列表（企业端，考虑company_id）
     */
    @Select("SELECT DISTINCT p.permission_code " +
            "FROM permissions p " +
            "INNER JOIN role_permissions rp ON p.id = rp.permission_id " +
            "INNER JOIN roles r ON rp.role_id = r.id " +
            "INNER JOIN user_role_company_relations ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND ur.company_id = #{companyId} AND p.is_active = 1 AND r.is_active = 1")
    List<String> getEnterprisePermissionsByUserId(@Param("userId") Long userId, @Param("companyId") Long companyId);
    
    /**
     * 根据用户ID和公司ID查询用户权限编码列表（用户端，考虑company_id）
     */
    @Select("SELECT DISTINCT p.permission_code " +
            "FROM permissions p " +
            "INNER JOIN role_permissions rp ON p.id = rp.permission_id " +
            "INNER JOIN roles r ON rp.role_id = r.id " +
            "INNER JOIN user_role_company_relations ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND ur.company_id = #{companyId} AND p.is_active = 1 AND r.is_active = 1")
    List<String> getUserPermissionsByUserId(@Param("userId") Long userId, @Param("companyId") Long companyId);
    
    /**
     * 根据用户ID查询用户权限编码列表（从primary_role_id获取，仅平台端权限）
     */
    @Select("SELECT DISTINCT p.permission_code " +
            "FROM permissions p " +
            "INNER JOIN role_permissions rp ON p.id = rp.permission_id " +
            "INNER JOIN roles r ON rp.role_id = r.id " +
            "INNER JOIN users u ON r.id = u.primary_role_id " +
            "WHERE u.id = #{userId} AND p.app = 'platform' AND p.is_active = 1 AND r.is_active = 1")
    List<String> getPermissionsByUserId(@Param("userId") Long userId);
    
    /**
     * 根据角色ID查询权限编码列表（仅平台端权限）
     */
    @Select("SELECT DISTINCT p.permission_code " +
            "FROM permissions p " +
            "INNER JOIN role_permissions rp ON p.id = rp.permission_id " +
            "WHERE rp.role_id = #{roleId} AND p.app = 'platform' AND p.is_active = 1")
    List<String> getPermissionsByRoleId(@Param("roleId") Long roleId);
    
    /**
     * 按端查询权限映射（编码 -> 名称）
     */
    @Select("SELECT id, permission_code, permission_name FROM permissions WHERE is_active = 1 AND app = #{app}")
    List<Permission> getAllActivePermissionsByApp(@Param("app") String app);

    @Select("SELECT * FROM permissions WHERE is_active = 1")
    List<Permission> getAllActivePermissions();
}
